package com.android.lovely.ui.dialog

import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.android.lovely.R
import com.android.lovely.databinding.DialogChatSetBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.manager.KVStore
import com.android.lovely.room.chat.ChatProfileData
import com.android.lovely.ui.chat.ChatDialogueSettingActivity
import com.android.lovely.ui.activity.CustomLanguagePickerActivity
import com.android.lovely.ui.activity.OptimizationActivity
import com.android.lovely.ui.chat.ChatMessageActivity
import com.android.lovely.ui.chat.ConversationViewModel
import com.android.lovely.ui.chat.getChatId
import com.android.lovely.user.UserManager
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.view.ViewExt.clickable
import kotlinx.coroutines.flow.collectLatest

/**
 *
 * @author why
 * @date 2025/3/28 18:57
 */
class ChatSetDialog : BaseDialogFragment<DialogChatSetBinding>() {
    private val viewModel by activityViewModels<ConversationViewModel> {
        viewModelFactory {
            initializer {
                ConversationViewModel(getChatId())
            }
        }
    }

    override fun initializeViews() {
        binding.translate.clickable {
            binding.translate.isSelected = !binding.translate.isSelected
            viewModel.autoTranslate = binding.translate.isSelected
        }
        binding.ivClose.clickable {
            dismissAllowingStateLoss()
        }
        binding.nameContainer.clickable {
            showProfileDialog()
        }
        binding.genderContainer.clickable {
            showProfileDialog()
        }
        binding.descContainer.clickable {
            showProfileDialog()
        }

        binding.modelContainer.clickable {
            if (viewModel.character.chatModel) {
                ChatModelDialog().apply {
                    arguments = bundleOf(ChatMessageActivity.CHAT_ID to this@ChatSetDialog.viewModel.botId)
                }.show(requireActivity().supportFragmentManager)
            }
            dismissAllowingStateLoss()
        }
        binding.styleContainer.clickable {
            startPage<ChatDialogueSettingActivity>(
                ChatMessageActivity.CHAT_ID to this@ChatSetDialog.viewModel.botId,
                "bg" to this@ChatSetDialog.viewModel.character.info.background.substringBefore(
                    "?"
                )
            )
            dismissAllowingStateLoss()
        }
        binding.reportContainer.clickable {
            startPage<OptimizationActivity>()
        }
        binding.resetContainer.clickable {
            requireActivity().showLovelyDialog{
                setDialogTitle(getString(R.string.save_model))
                setPositiveButton(getString(R.string.delete_record)) {
                    viewModel.clearConversation()
                    dismissAllowingStateLoss()
                }
                setNegativeButton(getString(R.string.keep_record)) {
                    dismissAllowingStateLoss()
                }
                setBottomClose(true)
            }
        }
        binding.languageContainer.clickable {
            startPage<CustomLanguagePickerActivity>()
        }

        lifecycleScope.launchSafety {
            viewModel.infoChangedFlow.collectLatest { profileData ->
                onProfile(profileData)
            }
        }

        binding.tvModel.text = when (viewModel.chatMode) {
            0 -> getString(R.string.passion)
            1 -> getString(R.string.tale)
            2 -> getString(R.string.dialogue)
            else -> ""
        }
        var currentLan = KVStore.speakLanguage

        binding.tvLanguage.text = currentLan.name
        binding.translate.isSelected = viewModel.autoTranslate

    }

    private fun showProfileDialog() {
        ChatProfileDialog().apply {
            arguments = bundleOf(ChatMessageActivity.CHAT_ID to this@ChatSetDialog.viewModel.botId)
        }.show(requireActivity().supportFragmentManager)
        dismissAllowingStateLoss()
    }

    override fun onResume() {
        super.onResume()
        var currentLan = KVStore.speakLanguage
        binding.tvLanguage.text = currentLan.name
    }

    private fun onProfile(item: ChatProfileData) {
        val profile = item.info
        binding.tvName.text = profile.chatName
        binding.tvGender.text =
            resources.getString(if (profile.chatGender == 1) R.string.male else if (profile.chatGender == 2) R.string.female else R.string.unknown)
        binding.tvDesc.text = profile.chatDec
        binding.modelContainer.isVisible = UserManager.isB() && item.chatModel

    }

    override fun isBottom(): Boolean {
        return true
    }
}